/* Ticket.cpp
 * Author: mtreece
 * Date Started: 02/09/2010
 * Purpose:
 *  Represents an individual "Ticket",
 *  which will consist of a fixed number
 *  of TicketLine objects.
 *  NB: A TicketLine object will consist
 *      of 5 numbers and a powerball number, 
 *      with limits defined in a .conf file.
 *  ***Quote by mtreece...
 *           "The only thing harder than
 *            roughing out a design, managing,
 *            using, modifying cryptic libraries,
 *            and debugging code of any language,
 *            is what to name an identifier."
 */

#include "Ticket.h"

unsigned int Ticket::nextSerial = 0;

Ticket::Ticket(unsigned int n){
	lines = new TicketLine*[n];
	nextSlot = 0;
	capacity = n;
	serial = ++nextSerial;
}

Ticket::~Ticket(){
	/* Free the memory from TicketLine objects
	 * after this Ticket has been destroyed;
	 * elsewise, extreme memory leakage.
	 */
	for(int i=0; i<nextSlot; i++)
		delete(lines[i]);
}

MoneyWorth* Ticket::getWorth(TicketLine* winner){
	unsigned int value = 0, jackpots = 0;
	MoneyWorth* current;
	for(int i=0; i<nextSlot; i++){
		current = lines[i]->getWorth(winner);
		value += current->value();
		jackpots += current->jackpot();
		delete(current);
	}

	/* Current implementation of MoneyWorth
	 * allows for non-{0,1} jackpot, for further
	 * data-exploration later, if need be.
	 */
	return new MoneyWorth(value, jackpots);
}

void Ticket::addTicketLine(TicketLine *x){
	/* TODO: Refactor to delete any
	 *       *known* TicketLine in this
	 *       spot... to save memory.
	 */
	if(nextSlot < capacity){
		lines[ nextSlot ] = x;
		nextSlot++;
	}
	else{
		// TODO: Throw exception?
	}
}

void Ticket::addRandomTicketLine(){
	addTicketLine( 
		new TicketLine( )
		 );
}

void Ticket::addAllRandom(){
	nextSlot = 0;
	for(int i=0; i<capacity; i++)
		addRandomTicketLine();
}

void Ticket::printTicket(){
	TicketLine** current = lines;
	printf("=== Ticket %9d " "============\n", serial);
	int num = 1;
	for(int i=0; i<nextSlot; i++){
		printf("| %2d) %s |\n", num++, (*current)->toString());
		*current++;
	}
	printf(TICKET_SEPARATOR "\n");
}

TicketLine* Ticket::getTicket(int i){
	return lines[i];
}

int Ticket::size(){
	return capacity;
}
